package cn.source.web.controller.zk;

import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.core.domain.entity.SysRole;
import cn.source.common.core.domain.entity.SysUser;
import cn.source.common.core.page.TableDataInfo;
import cn.source.common.utils.SecurityUtils;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.system.service.ISysRoleService;
import cn.source.system.service.ISysUserService;
import cn.source.zk.domain.entity.SimCard;
import cn.source.zk.domain.vo.SimSortVO;
import cn.source.zk.service.IZkSimCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/zk/simCard")
public class zkSimCardController extends BaseController {

    @Autowired
    private IZkSimCardService simCardService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询sim卡分页列表
     * @param simCard
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo listPage(SimCard simCard){

        List<SysRole> sysRoles = roleService.selectRolesListByUserId(SecurityUtils.getUserId());
        if (sysRoles.size() > 0) {
            SysRole sysRole = sysRoles.get(0);
            // 一级管理员
            if (sysRole.getRoleKey().equals("company")) {
                SysUser sysUserQueryParams = new SysUser();
                sysUserQueryParams.setCreateBy(SecurityUtils.getUsername());
                List<SysUser> sysUsers = userService.selectUserList(sysUserQueryParams);
                String createByStr = SecurityUtils.getUsername();
                if (sysUsers.size() > 0) {
                    String bufferStr = sysUsers.stream().map(m -> m.getUserName()).collect(Collectors.joining(","));
                    createByStr = createByStr + ',' + bufferStr;
                }
                simCard.setCreateBy(createByStr);
            }
            // 普通管理员
            if (sysRole.getRoleKey().equals("common")) {
                simCard.setCreateBy(SecurityUtils.getUsername());
            }

            // 一般人员
            if (sysRole.getRoleKey().equals("general")) {
                simCard.setCreateBy(SecurityUtils.getUsername());
            }
        }
        startPage();
        List<SimCard> list = simCardService.listPage(simCard);
        return getDataTable(list);

    }

    /**
     * 新增sim卡
     * @param simCard
     * @return
     */
    @PostMapping
    public AjaxResult save(@RequestBody SimCard simCard){

        // 检查sim卡是否存在
        if (simCardService.checkSimCard(simCard.getPhone(),null)) {
            return AjaxResult.error("SIM卡"+simCard.getPhone()+",已存在");
        }

        simCard.setCreateBy(getUsername());
        simCard.setUpdateBy(getUsername());

        return toAjax(simCardService.save(simCard));
    }

    /**
     * 根据num获取sim卡信息
     * @return
     */
    @GetMapping("/{num}")
    public AjaxResult getByNum(@PathVariable Long num){
        SimCard simCard = simCardService.getByNum(num);
        return AjaxResult.success(simCard);
    }

    /**
     * 修改sim卡信息
     * @param simCard
     * @return
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SimCard simCard){
        // 检查sim卡是否存在
        if (simCardService.checkSimCard(simCard.getPhone(),simCard.getNum())) {
            return AjaxResult.error("SIM卡"+simCard.getPhone()+",已存在");
        }

        simCard.setUpdateBy(getUsername());
        return toAjax(simCardService.edit(simCard));
    }

    /**
     * 批量删除sim信息
     */
    @DeleteMapping("/{nums}")
    public AjaxResult removeBatch(@PathVariable List<Long> nums){
        return toAjax(simCardService.removeBatch(nums));
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, SimCard simCard){
        List<SimCard> list = simCardService.listPage(simCard);
        ExcelUtil<SimCard> util = new ExcelUtil<SimCard>(SimCard.class);
        util.exportExcel(response, list, "sim卡信息");
    }

    @GetMapping("/getSimSortList")
    public AjaxResult getSimSortList(){

        List<SimSortVO>list = simCardService.getSimSortList();

        return AjaxResult.success(list);

    }

    /**
     * 获取到期数据15天
     * @return
     */
    @GetMapping("/getExpiringData")
    public AjaxResult getExpiringData() {
        List<SimCard> list = simCardService.getExpiringData();
        return  AjaxResult.success(list);
    }



}
